unit clsOrc_Departamento;

interface

uses Dm,dbtables,biblio,sysutils,Grids;

type
  TOrc_Departamento=Class(TObject)
    public
      Orc_Valor: double;
      function  Recuperar(Dep_Codigo,Orc_MesAno: string): boolean;
      function  Retorna_Movtos(Orc_MesAno: string; var sg: TStringGrid): boolean;
      procedure Gravar(Dep_Codigo,Orc_MesAno: string);
      procedure Excluir(Orc_MesAno: string);
    end;

implementation

procedure TOrc_Departamento.Gravar;
var tq: TQuery;
begin
 Orc_MesAno     := copy(Orc_MesAno,1,2)+copy(Orc_MesAno,4,4);
 tq             := Tquery.Create(DmInt);
 tq.databasename:= dmint.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from Orcamento_Depto where Dep_Codigo=:a and Orc_MesAno=:b');
 tq.params[0].asstring:= Dep_Codigo;
 tq.params[1].asstring:= Orc_MesAno;
 tq.open;
 if tq.eof then begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into Orcamento_Depto values ('+
             ':k_Dep_Codigo,'+
             ':k_Orc_MesAno,'+
             ':k_Orc_Valor)');
 end else begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update Orcamento_Depto set   '+
             'Orc_Valor  =:k_Orc_Valor     '+
             'where                        '+
             'Dep_Codigo=:k_Dep_Codigo and '+
             'Orc_MesAno=:k_Orc_MesAno     ');
 end;
 tq.parambyname('k_Dep_Codigo').asstring:= Dep_Codigo;
 tq.parambyname('k_Orc_MesAno').asstring:= Orc_MesAno;
 tq.parambyname('k_Orc_Valor').asfloat  := Orc_Valor;
 tq.execsql;
 tq.close;
 tq.Free;
end;

procedure TOrc_Departamento.Excluir;
var tq: TQuery;
begin
 Orc_MesAno     := copy(Orc_MesAno,1,2)+copy(Orc_MesAno,4,4);
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Delete from Orcamento_Depto where Orc_MesAno=:a');
 tq.params[0].asstring:= Orc_MesAno;
 tq.execsql;
 tq.close;
 tq.Free;
end;

function TOrc_Departamento.Recuperar;
var tq: TQuery;
begin
 Orc_MesAno     := copy(Orc_MesAno,1,2)+copy(Orc_MesAno,4,4);
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Orcamento_Depto where Dep_Codigo=:a and Orc_MesAno=:b');
 tq.params[0].asstring:= Dep_Codigo;
 tq.params[1].asstring:= Orc_MesAno;
 tq.open;
 if tq.eof then result:= false else result:= true;
 Orc_Valor:= tq.fieldbyname('Orc_Valor').asfloat;
 tq.close;
 tq.free;
end;

function TOrc_Departamento.Retorna_Movtos;
var tq   : TQuery;
    i    : integer;
    achou: boolean;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Departamento');
 tq.open;
 i    := 1;
 achou:= false;
 while not tq.eof do begin
  if i>1 then sg.rowcount:= sg.rowcount+1;
  sg.cells[0,i]:= tq.fieldbyname('Dep_Codigo').asstring+' - '+tq.fieldbyname('Dep_Descricao').asstring;
  Orc_Valor    := 0;
  if Recuperar(tq.fieldbyname('Dep_Codigo').asstring,Orc_MesAno) then achou:= true;
  sg.cells[1,i]:= Formata_Valor(Orc_Valor);
  sg.cells[2,i]:= Formata_Valor(0);
  sg.cells[3,i]:= Formata_Valor(0);
  i            := i+1;
  tq.next;
 end;
 result:= achou;
 tq.close;
 tq.free;
end;

end.
